package com.ddm.config;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.common.auth.CredentialsProvider;
import com.aliyun.oss.common.auth.DefaultCredentialProvider;
import com.ddm.service.IStorage;
import com.ddm.service.impl.AliyunStorage;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Created by he on 2025/5/18 16:41
 */
@Configuration
@ConditionalOnProperty(prefix = "oss.aliyun", name = "active", havingValue = "true")
@ConditionalOnClass(OSSClient.class)
public class AliyunConfiguration {
    @Bean
    @Qualifier("aliyunStorage")
    IStorage aliyunStorage(OssProperties ossProperties) {
        OssProperties.Aliyun aliyun = ossProperties.getAliyun();
        CredentialsProvider credentialsProvider = new DefaultCredentialProvider(aliyun.getAccessKeyId(),aliyun.getAccessKeySecret());
        OSS ossClient = OSSClientBuilder.create()
                .endpoint(aliyun.getEndpoint())
                .credentialsProvider(credentialsProvider)
                .build();
        return new AliyunStorage(ossClient);
    }

}
